package com.dev.lamp;

import java.util.Date;

import com.analysis.StoreHouse;
import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechUnderstander;
import com.iflytek.cloud.speech.SpeechUnderstanderListener;
import com.iflytek.cloud.speech.SpeechUtility;
import com.iflytek.cloud.speech.UnderstanderResult;

public class Understander {

	private SpeechUnderstander understander;

	public Understander() {
		SpeechUtility.createUtility(SpeechConstant.APPID + "=569dcce8");
		// 语音听写对象
		this.understander = SpeechUnderstander.createUnderstander();
		// 2.设置参数，语义场景配置请登录http://osp.voicecloud.cn/
		this.understander.setParameter(SpeechConstant.DOMAIN, "iat");  
		this.understander.setParameter(SpeechConstant.LANGUAGE, "zh_cn");  
		this.understander.setParameter(SpeechConstant.ACCENT, "mandarin");  
		// 3.开始语义理解
		understander.startUnderstanding(mUnderstanderListener);
	}

	// XmlParser为结果解析类，见SpeechDemo
	private SpeechUnderstanderListener mUnderstanderListener = new SpeechUnderstanderListener() {
		public void onResult(UnderstanderResult result) {
			String text = result.getResultString();
			System.out.println(new Date().getTime() +"Unde =" + text);
			StoreHouse.addUnder(text);
			goon();
		}

		public void onError(SpeechError error) {
			// if (null != error) {
			// error.getErrorDescription(true); // 获取错误码描述
			// }
			goon();
		}// 会话发生错误回调接口

		public void onBeginOfSpeech() {
		}// 开始录音

		public void onVolumeChanged(int volume) {
		} // 音量值0~30

		public void onEndOfSpeech() {
		}// 结束录音

		public void onEvent(int eventType, int arg1, int arg2, String msg) {
		}// 扩展用接口
	};

	private boolean goon = true;

	private void goon() {
		if (this.goon) {
			this.understander.startUnderstanding(mUnderstanderListener);
		} else {
			this.understander.stopUnderstanding();
		}
	}

}
